package com.ychs.service;

import com.ychs.dao.MemberMapper;
import com.ychs.entity.Member;
import com.ychs.entity.Member;
import com.ychs.util.Constants;
import com.ychs.util.DBUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:成员业务实现类
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/5/23
 */
public class MemberService implements IBaseService<Member>{
    /**
     * 添加记录
     *
     * @param member 记录对象
     * @return 是否成功
     */
    @Override
    public boolean add(Member member) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        int result = mapper.insert(member);
        session.commit();
        //session.close();
        DBUtil.close(session);
        return result== Constants.UPDATE_SUCCESS;
    }

    /**
     * 修改记录
     *
     * @param member 修改记录对象
     * @return 成功与否
     */
    @Override
    public boolean modify(Member member) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        int result = mapper.update(member);
        session.commit();
        //session.close();
        DBUtil.close(session);
        return result== Constants.UPDATE_SUCCESS;
    }

    /**
     * 删除记录
     *
     * @param id 待删除记录的主键
     * @return 是否成功
     */
    @Override
    public boolean remove(int id) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        int result = mapper.delete(id);
        session.commit();
        //session.close();
        DBUtil.close(session);
        return result== Constants.UPDATE_SUCCESS;
    }

    /**
     * 查询单个对象
     *
     * @param id 查询对象的主键
     * @return 查询结果
     */
    @Override
    public Member searchById(int id) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        Member member = mapper.selectById(id);
        //session.close();
        DBUtil.close(session);
        return member;
    }

    /**
     * 查询多条记录
     *
     * @param conditions 查询条件
     * @return 查询结果
     */
    @Override
    public List<Member> search(Map<String, Object> conditions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        List<Member> members= mapper.select(conditions);
        //session.close();
        DBUtil.close(session);
        return members;
    }

    /**
     * 按照条件查询记录总条数
     * @param conditions 查询条件
     * @return 查询结果
     */
    @Override
    public int searchTotalNumber(Map<String, Object> conditions) {
        SqlSession session = DBUtil.getInstance().getSqlSession();
        MemberMapper mapper = session.getMapper(MemberMapper.class);
        int count= mapper.selectTotalNumber(conditions);
        //session.close();
        DBUtil.close(session);
        return count;
    }
}
